/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.web.validating;


import com.andersen.web.beans.UserCommandForm;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

/**
 *
 * @author Corvus 11
 */
@Component
public class EditUserValidator extends AbstractValidator{

    @Override
    public boolean supports(Class type) {
        return UserCommandForm.class.isAssignableFrom(type);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "required.login", "Field is required.");
        
        UserCommandForm fmt = (UserCommandForm)obj;
        
        if (fmt.getLogin().equals(fmt.getPrevLogin())
                && fmt.getPassword().isEmpty()
                && fmt.getPassword().isEmpty()
                && fmt.getConfirmPassword().isEmpty()) {
            errors.rejectValue("prevLogin", "hidden.nothingchanged");
            return;
        }
        
        if (fmt.getLogin()!=null && !fmt.getLogin().isEmpty()) {
            if (fmt.getLogin().matches("^.*[^a-zA-Z0-9_\\-.].*$")) {
                errors.rejectValue("login", "incorrect.login");
            } else if ((!fmt.getPrevLogin().equals(fmt.getLogin()) && 
                        !userService.isLoginFree(fmt.getLogin())))
                    errors.rejectValue("login", "alreadyusing.login");
        }
        
        if (!fmt.getPassword().isEmpty() || !fmt.getPreviousPassword().isEmpty() || !fmt.getConfirmPassword().isEmpty()) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required.password", "Field is required.");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword", "required.confirmPassword", "Field is required.");
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "previousPassword", "required.previousPassword", "Field is required.");
        }
  
        if (!fmt.getPassword().isEmpty()) {
            if (!fmt.getPassword().equals(fmt.getConfirmPassword())) {
                errors.rejectValue("password", "notmatch.password");
            } else {
                Md5PasswordEncoder enc = new Md5PasswordEncoder();
                String confirmation = enc.encodePassword(fmt.getPreviousPassword(), null);
                String dbPassHash = userService.getUser(fmt.getId()).getPassword();
                if (!dbPassHash.equals(confirmation)) {
                        errors.rejectValue("previousPassword", "notmatch.previouspassword");
                    }
                }
            }
        }
            
        
        /*if (!(fmt.isAdmin() || fmt.isHr() || fmt.isManager() || fmt.isSimpleUser())) {
            errors.rejectValue("hr", "required.anyRole");
        }*/
        
    }
